package com.windy.i18n.config;

import com.windy.i18n.constant.LocaleType;
import com.windy.i18n.interceptor.LocaleInterceptor;
import com.windy.i18n.resolver.LangHeaderLocaleResolver;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * Created by liyufeng on 2022/5/15
 */
@Configuration
@EnableConfigurationProperties(I18nProperties.class)
public class I18nAutoConfiguration {

    @Bean
    public MessageSource messageSource(I18nProperties properties) {
        ReloadableResourceBundleMessageSource messageBundle = new ReloadableResourceBundleMessageSource();
        messageBundle.setBasename(properties.getBasename());
        messageBundle.setDefaultEncoding(properties.getDefaultEncoding());
        messageBundle.setConcurrentRefresh(properties.isConcurrentRefresh());
        messageBundle.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
        messageBundle.setCacheSeconds(properties.getCacheSeconds());
        return messageBundle;
    }

    @Bean
    public MessageSourceAccessor getDefaultMessageSourceAccessor(MessageSource messageSource) {
        MessageSourceAccessor accessor = new MessageSourceAccessor(messageSource);
        return accessor;
    }


    @Configuration(proxyBeanMethods = false)
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    @AutoConfigureBefore(WebMvcAutoConfiguration.class)
    public static class I18nWebMvcConfiguration{

        @Bean(name = "localeResolver")
        public LocaleResolver getLocalResolver(I18nProperties properties) {
            LangHeaderLocaleResolver resolver = new LangHeaderLocaleResolver();
            resolver.setSupportedLocales(Arrays.stream(properties.getSupportedLocales().split(","))
                    .map(LocaleType::getValue)
                    .collect(Collectors.toList()));
            resolver.setDefaultLocale(LocaleType.getValue((properties.getDefaultLocale())));
            resolver.setHeaderName(properties.getInterceptor().getName());
            return resolver;
        }

        @Bean
        public LocaleInterceptor localeInterceptor(I18nProperties properties){
            LocaleInterceptor localeInterceptor = new LocaleInterceptor();
            localeInterceptor.setProper(properties.getInterceptor());
            return localeInterceptor;
        }
        @Bean
        public WebMvcConfigurer webMvcConfigurer(I18nProperties properties){
            return new WebMvcConfigurer() {
                @Override
                public void addInterceptors(InterceptorRegistry registry) {

                    registry.addInterceptor(localeInterceptor(properties))
                            .addPathPatterns(properties.getInterceptor().getPatterns().split(","));
                }
            };
        }

    }

}
